﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using MidiInterface;

namespace DrummerBotTest
{
    public partial class DrummerBotTest : Form
    {
        private bool running;
        private Thread serialThread;
        private Thread pitchBarThread;
        private Midi midi;

        public DrummerBotTest()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            midi = new Midi("COM7");
            running = true;
            pitchBarThread = new Thread(new ThreadStart(PitchBarThread));
            pitchBarThread.Start();
            serialThread = new Thread(new ThreadStart(SerialThread));
            serialThread.Start();
        }

        private void SerialThread()
        {
            while (running)
            {
                textBox.AppendText(midi.Receive());

                Thread.Sleep(10);
            }
        }

        private void PitchBarThread()
        {
            byte[] bytes = new byte[3];
            int i = 0;
            int lastPitch = pitchBar.Value;
            while (running)
            {
                Thread.Sleep(10);
                if (midi.IsConnected && lastPitch != pitchBar.Value)
                {
                    i = 0;
                    lastPitch = pitchBar.Value;
                    if (midi.LastCommand != MidiCommand.PitchBend)
                    {
                        bytes[i] = 0xE0;
                        i++;
                        midi.LastCommand = MidiCommand.PitchBend;
                    }
                    bytes[i] = Convert.ToByte((lastPitch >> 7) & 0x7F);
                    i++;
                    bytes[i] = Convert.ToByte(lastPitch & 0x7F);
                    i++;
                    midi.Send(bytes, i);
                }
            }
        }

        private void DrummerBotTest_FormClosing(object sender, FormClosingEventArgs e)
        {
            running = false;
            if (!serialThread.Join(20))
            {
                serialThread.Abort();
            }
            if (!pitchBarThread.Join(20))
            {
                pitchBarThread.Abort();
            }
            midi.Disconnect();
        }

        private void bassButton_Click(object sender, EventArgs e)
        {
            midi.SendNote(0, 127);
        }

        private void mainButton_Click(object sender, EventArgs e)
        {
            midi.SendNote(1, 127);
        }

        private void hihatButton_Click(object sender, EventArgs e)
        {
            midi.SendNote(2, 127);
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            if (midi.IsConnected)
            {
                midi.Disconnect();
                connectButton.Text = "Connect";
            }
            else
            {
                midi.Connect();
                connectButton.Text = "Disconnect";
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            textBox.Focus();
            textBox.SelectionStart = textBox.Text.Length;
            textBox.ScrollToCaret();
        }
    }
}
